class OrdersController < ApplicationController
  # GET /orders
  # GET /orders.json
  # allows the index page for orders to work
  def index
    @orders = Order.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @orders }
    end
  end

  # GET /orders/1
  # GET /orders/1.json
  # allows the show page for orders to work
  def show
    @order = Order.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @order }
    end
  end

  # GET /orders/new
  # GET /orders/new.json
  # allows the new page for orders to work
  def new
    @order = Order.new
    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @order }
    end
  end

  # GET /orders/1/edit
  # allows the edit page for orders to work
  def edit
    @order = Order.find(params[:id])
  end

  # POST /orders
  # POST /orders.json
  # allows the creation of a new order
  def create
    @order = Order.new(params[:order])
    # set this order as belonging to current user
    @order.user_id = session[:user_id]
   
    respond_to do |format|
      if @order.save
        format.html { redirect_to @order, :notice => 'Order was successfully created.' }
        format.json { render :json => @order, :status => :created, :location => @order }
      else
        format.html { render :action => "new" }
        format.json { render :json => @order.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /orders/1
  # PUT /orders/1.json
  # allows the updating of an order
  def update
    @order = Order.find(params[:id])

    respond_to do |format|
      if @order.update_attributes(params[:order])
        format.html { redirect_to @order, :notice => 'Order was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @order.errors, :status => :unprocessable_entity }
      end
    end
  end

  # GET /orders/1/driver_feedback
  # allows leaving feedback for the driver
  def driver_feedback
    @order = Order.find(params[:id])
    # ensures that the logged in user is the right one
    if @order.user_id = current_user.id
      respond_to do |format|
        format.html # driver_feedback.html.erb
        format.json { render :json => @order }
      end
    else
      # failure message
      respond_to do |format|
        format.html { redirect_to root_url, :notice => 'You are not authorized to leave feedback for the driver.' }
        format.json { head :ok }
      end
    end
  end

  # GET /orders/1/customer_feedback
  # allows leaving feedback for the customer
  def customer_feedback
    @order = Order.find(params[:id])
    # ensures taht the logged in user is the right one
    if @order.driver_id = current_user.id
      respond_to do |format|
        format.html # customer_feedback.html.erb
        format.json { render :json => @order }
      end
    else
      # failure message
      respond_to do |format|
        format.html { redirect_to root_url, :notice => 'You are not authorized to leave feedback for the customer.' }
        format.json { head :ok }
      end
    end
  end

  # POST /orders/1
  # allows a driver to take an order
  def take
   @order = Order.find(params[:id])
   # set current user as driver
   @order.driver_id = current_user.id
   @order.save # need to explicitly save order
    respond_to do |format|
      format.html { redirect_to :back, :notice => 'Order successfully taken.' }
      format.json { head :ok }
    end
  end

  # DELETE /orders/1
  # DELETE /orders/1.json
  # deletes an order
  def destroy
    @order = Order.find(params[:id])
    # ensures taht the logged in user is the right one
    if @order.user_id = current_user.id || current_user.isadmin
      @order.destroy
      respond_to do |format|
        format.html { redirect_to root_url, :notice => 'Order deleted sucessfully.'}
        format.json { head :ok }
      end
    else
      # failure message
      respond_to do |format|
        format.html { redirect_to root_url, :notice => 'You are not authorized to delete this order.' }
        format.json { head :ok }
      end
    end
  end

  # POST /orders/1
  # marks an order as failed
  def failed
    @order = Order.find(params[:id])
    # ensures taht the logged in user is the right one
    if @order.user_id = current_user.id
      @order.delivery_failed = true
      @order.save
      respond_to do |format|
        format.html { redirect_to root_url, :notice => 'Order reported as failed.' }
        format.json { head :ok }
      end
    else
      # failure message
      respond_to do |format|
        format.html { redirect_to root_url, :notice => 'You are not authorized to fail this order.' }
        format.json { head :ok }
      end
    end
  end

  # POST /orders/1
  # marks an order as completed
  def completed
    @order = Order.find(params[:id])
    # ensures taht the logged in user is the right one
    if @order.driver_id = current_user.id
      @order.delivery_completed = true
      @order.save
      respond_to do |format|
        format.html { redirect_to root_url, :notice => 'Order completed sucessfully.' }
        format.json { head :ok }
      end
    else
      # failure message
      respond_to do |format|
        format.html { redirect_to root_url, :notice => 'You are not authorized to complete this order.' }
        format.json { head :ok }
      end
    end
  end
end
